/*
	Copyright (c) 2004 Cory Nelson

	Permission is hereby granted, free of charge, to any person obtaining
	a copy of this software and associated documentation files (the
	"Software"), to deal in the Software without restriction, including
	without limitation the rights to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to
	permit persons to whom the Software is furnished to do so, subject to
	the following conditions:

	The above copyright notice and this permission notice shall be included
	in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
	IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
	CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
	TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
	SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef __INTERNAL_H__
#define __INTERNAL_H__

// keep platform specific stuff here.

#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <netdb.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
typedef int SOCKET;
#define SOCKET_ERROR (-1)
#define INVALID_SOCKET (-1)
#define closesocket(s) close(s)
#define stricmp(x,y) strcasecmp(x,y)
#endif
#include <stddef.h>
#include "gamestat.h"

// most servers return stuff in little-endian so make big-endian systems have to swap bits.
#ifdef BIG_ENDIAN
#define swapint16(A)  ((((unsigned short)(A) & 0xff00) >> 8) | \
							 (((unsigned short)(A) & 0x00ff) << 8))
#define swapint32(A)  ((((unsigned int)(A) & 0xff000000) >> 24) | \
							 (((unsigned int)(A) & 0x00ff0000) >> 8)  | \
							 (((unsigned int)(A) & 0x0000ff00) << 8)  | \
							 (((unsigned int)(A) & 0x000000ff) << 24))
#else
#define swapint16(A) (A)
#define swapint32(A) (A)
#endif

SOCKET getsock(const char *host, unsigned short port, int family, int socktype, int protocol);
#define getsockudp(host,port) getsock(host, port, AF_INET, SOCK_DGRAM, IPPROTO_UDP)
#define getsocktcp(host,port) getsock(host, port, AF_INET, SOCK_STREAM, IPPROTO_TCP)

char *getpacket(SOCKET s, size_t *len);

GS_SERVERINFO *queryserver_ase(const char *host, unsigned short port);
GS_SERVERINFO *queryserver_doom3(const char *host, unsigned short port);
GS_SERVERINFO *queryserver_gamespy(const char *host, unsigned short port);
GS_SERVERINFO *queryserver_gamespy2(const char *host, unsigned short port);
GS_SERVERINFO *queryserver_ghostrecon(const char *host, unsigned short port);
GS_SERVERINFO *queryserver_halflife(const char *host, unsigned short port);
GS_SERVERINFO *queryserver_moh(const char *host, unsigned short port);
GS_SERVERINFO *queryserver_quakeworld(const char *host, unsigned short port);
GS_SERVERINFO *queryserver_quake2(const char *host, unsigned short port);
GS_SERVERINFO *queryserver_quake3(const char *host, unsigned short port);
GS_SERVERINFO *queryserver_quake4(const char *host, unsigned short port);
GS_SERVERINFO *queryserver_savage(const char *host, unsigned short port);
GS_SERVERINFO *queryserver_sof(const char *host, unsigned short port);
GS_SERVERINFO *queryserver_sof2(const char *host, unsigned short port);
GS_SERVERINFO *queryserver_source(const char *host, unsigned short port);
GS_SERVERINFO *queryserver_tribes(const char *host, unsigned short port);
GS_SERVERINFO *queryserver_tribes2(const char *host, unsigned short port);

GS_SERVER *querymaster_doom3(const char *host, unsigned short port, unsigned short filter);
GS_SERVER *querymaster_quake2(const char *host, unsigned short port);
GS_SERVER *querymaster_quake3(const char *host, unsigned short port, unsigned short protocol, unsigned short filter);
GS_SERVER *querymaster_sof2(const char *host, unsigned short port, unsigned short protocol, unsigned short filter);
GS_SERVER *querymaster_stvef(const char *host, unsigned short port, unsigned short protocol, unsigned short filter);
GS_SERVER *querymaster_swjk2(const char *host, unsigned short port, unsigned short protocol, unsigned short filter);
GS_SERVER *querymaster_steam(const char *host, unsigned short port, const char *game, unsigned short filter);

void stripcolor_null(char *out, const char *in);
void stripcolor_quakeworld(char *out, const char *in);
void stripcolor_quake3(char *out, const char *in);
void stripcolor_sof(char *out, const char *in);

#ifdef _DEBUG
void dbg_dumpbuf(const char *file, const void *buf, size_t size);
#else
#define dbg_dumpbuf(x,y,z)
#endif

extern unsigned short lasterror;
extern struct timeval socktimeout;

#endif
